package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.AttendanceRequestDTO;
import com.tianya.domain.entity.Attendance;
import com.tianya.domain.entity.Parent;
import com.tianya.domain.entity.Student;
import com.tianya.domain.vo.AttendanceVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.service.*;
import com.tianya.mapper.AttendanceMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【attendance(考勤表)】的数据库操作Service实现
* @createDate 2025-05-24 18:58:19
*/
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
    implements AttendanceService{
    @Resource
    private StudentService studentService;
    @Resource
    private CourseService courseService;
    @Resource
    private ClazzService clazzService;

    @Resource
    private ParentService parentService;
    @Override
    public void createAttendance(AttendanceRequestDTO attendanceRequestDTO) {
        Attendance attendance = Attendance.builder()
                .studentId(attendanceRequestDTO.getStudentId())
                .courseId(attendanceRequestDTO.getCourseId())
                .date(attendanceRequestDTO.getDate())
                .status(attendanceRequestDTO.getStatus())
                .remark(attendanceRequestDTO.getRemark())
                .build();
        save(attendance);
    }

    @Override
    public PageResultVO<AttendanceVO> pageAttendance(Integer pageNum, Integer pageSize, AttendanceRequestDTO attendanceRequestDTO) {
        Page<Attendance> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(attendanceRequestDTO.getDate()!=null,Attendance::getDate, attendanceRequestDTO.getDate())
                .eq(attendanceRequestDTO.getClazzId()!=null,Attendance::getClazzId, attendanceRequestDTO.getClazzId());
        Page<Attendance> attendancePage = page(page, queryWrapper);
        List<AttendanceVO> attendanceVOS = new ArrayList<>();
        for (Attendance attendance : attendancePage.getRecords()) {
            Long studentID = attendance.getStudentId();
            Long courseId = attendance.getCourseId();
            Long clazzId = attendance.getClazzId();
            Parent parent = parentService.getOne(new QueryWrapper<Parent>().eq("student_id", studentID));
            Long parentId = parent!=null?parent.getParentId():null;
            Student student = studentService.getById(studentID);
            String studentName = student!=null?student.getName():"unKnown";
            String courseName = courseService.getById(courseId)!=null?courseService.getById(courseId).getName():"unKnown";
            String clazzName = clazzService.getById(clazzId)!=null?clazzService.getById(clazzId).getName():"unKnown";
            AttendanceVO attendanceVO = new AttendanceVO();
            BeanUtil.copyProperties(attendance, attendanceVO);
            attendanceVO.setParentId(parentId);
            attendanceVO.setStudentName(studentName);
            attendanceVO.setCourseName(courseName);
            attendanceVO.setClazzName(clazzName);
            attendanceVOS.add(attendanceVO);
        }
        return new PageResultVO<>(attendancePage.getTotal(), attendanceVOS);
    }

    @Override
    public AttendanceVO getAttendanceByStuId(Long id) {
        Attendance attendance = getOne(new QueryWrapper<Attendance>().eq("student_id", id));
        Long studentID = attendance.getStudentId();
        Long courseId = attendance.getCourseId();
        Long clazzId = attendance.getClazzId();
        Parent parent = parentService.getOne(new QueryWrapper<Parent>().eq("student_id", studentID));
        Long parentId = parent!=null?parent.getParentId():null;
        Student student = studentService.getById(studentID);
        String studentName = student!=null?student.getName():"unKnown";
        String courseName = courseService.getById(courseId)!=null?courseService.getById(courseId).getName():"unKnown";
        String clazzName = clazzService.getById(clazzId)!=null?clazzService.getById(clazzId).getName():"unKnown";
        AttendanceVO attendanceVO = new AttendanceVO();
        BeanUtil.copyProperties(attendance, attendanceVO);
        attendanceVO.setParentId(parentId);
        attendanceVO.setStudentName(studentName);
        attendanceVO.setCourseName(courseName);
        attendanceVO.setClazzName(clazzName);
        return attendanceVO;

    }
}




